package com.playball.lottery.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.playball.lottery.R;
import com.playball.lottery.bean.OrderBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**确认订单适配器
 * Created by Administrator on 2017/2/17.
 */

public class OrderAdapter  extends BaseAdapter {
    public Context context;
    private LayoutInflater inflater;
    public ArrayList<OrderBean> list;
    private List<Map<String, String>> mData; // 存储的editTex值
    private static HashMap<Integer, Boolean> isSelected;
    private Map<String, String> editorValue = new HashMap<String, String>();


    public  OrderAdapter(Context context,ArrayList<OrderBean>  list){
        this.context=context;
        this.list=list;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return  list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public void setData( List<Map<String, String>> data) {
        mData = data;
        init();
    }

    private void init() {
        editorValue.clear();
        isSelected = new HashMap<Integer, Boolean>();
        if (mData != null) {
            for (int i = 0; i < mData.size(); i++) {
                isSelected.put(i, false);
            }
        }
    }
    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.danqiu_order_list, viewGroup, false);
            viewHolder.tv_num=(TextView)convertView.findViewById(R.id.tv_num);
            viewHolder.tv_pei=(TextView)convertView.findViewById(R.id.tv_pei);
            viewHolder.ed_num=(EditText)convertView.findViewById(R.id.ed_num);
            viewHolder.checkbox=(CheckBox)convertView.findViewById(R.id.checkbox);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        OrderBean bean=list.get(i);

        String  msg=bean.getBetContent().replace("1=","").replaceAll("[a-zA-Z]"," ");

        viewHolder.tv_num.setText(msg);
        viewHolder.tv_pei.setText(bean.getBetOdds());
        viewHolder.ed_num.setText(bean.getBetMoney());
//        if(viewHolder.checkbox.isChecked()==false){
//            Toast.makeText(context,"请确认订单信息！",Toast.LENGTH_LONG).show();
//        }


//        class MyTextWatcher implements TextWatcher {
//
//            public MyTextWatcher(ViewHolder holder) {
//                mHolder = holder;
//            }
//
//            private ViewHolder mHolder;
//
//            @Override
//            public void beforeTextChanged(CharSequence s, int start,
//                                          int count, int after) {
//                // TODO Auto-generated method stub
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start,
//                                      int before, int count) {
//                // TODO Auto-generated method stub
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                // TODO Auto-generated method stub
//                if (s != null && !"".equals(s.toString())) {
//                    int position = (Integer) mHolder.ed_num.getTag();
//                    // 当EditText数据发生改变的时候存到data变量中
//                    mData.get(position).put("list_item_inputvalue",
//                            s.toString());
//                }
//            }
//        }
//        viewHolder.ed_num.addTextChangedListener(new MyTextWatcher(viewHolder));


//        viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                // TODO Auto-generated method stub
//                int position = (Integer) viewHolder.ed_num.getTag();
//                isSelected.put(position, isChecked);
//            }
//        });
        // viewHolder.checkbox.setChecked(isSelected.get(i));

        return convertView;
    }

    public HashMap<Integer,Boolean> getIsSelected() {
        return isSelected;
    }

    public void setIsSelected(HashMap<Integer,Boolean> isSelected) {
        this.isSelected = isSelected;
    }

    public class ViewHolder {
        TextView tv_num;//号码
        TextView tv_pei;//赔率
        EditText ed_num;//金额
        CheckBox checkbox;//确认
    }
}
